package com.example.leave.entity;

import com.example.leave.common.DateTimeUtil;
import com.example.leave.common.JwtUtil;
import com.example.leave.common.UuidUtil;
import com.example.leave.exception.BizException;
import com.example.leave.form.LeaveFormForm;
import com.example.leave.pojo.StartEndTimestamp;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;

@Data
@Entity
@Table(name = "ru_leave_form", schema = "public")
public class LeaveForm {
    @Id
    private String id;

    @Column(name = "category")
    private String category;

    @Column(name = "reason")
    private String reason;
    @Column(name = "start_time")
    private Timestamp startTime;

    @Column(name = "end_time")
    private Timestamp endTime;

    @Column(name = "create_time")
    private Timestamp createTime;

    @Column(name = "user_id")
    private String userId;


    public LeaveForm() {

    }

    public LeaveForm(LeaveFormForm leaveFormForm) {
        this.userId = JwtUtil.getUserFromToken().getId();
        if (null == this.userId) {
            throw new BizException("请重新登录");
        }
        this.id = UuidUtil.getUuid32();
        this.category = leaveFormForm.getCategory();
        this.reason = leaveFormForm.getReason();
        this.createTime = new Timestamp(System.currentTimeMillis());
        StartEndTimestamp startEndTimestamp = DateTimeUtil.startEndDateTimeVoToStartEndTimestamp(leaveFormForm);
        this.startTime = startEndTimestamp.getStartTime();
        this.endTime = startEndTimestamp.getEndTime();
        if (!this.startTime.before(this.endTime)) {
            throw new BizException("参数不合法，结束时间应该晚于开始时间！");
        }
    }


}
